﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Help;
using Model;

namespace BLLA
{
    public partial class Order_FormControl_T : BaseBLL<Model.Order_FormControl_T>, IBLL.IOrder_FormControl_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();

        /// <summary>
        /// 查询订单状态控制表
        /// </summary>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string GetOrder_FormControl(string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                #region 执行操作
                List<Model.Order_FormControl_T> ls = new List<Model.Order_FormControl_T>();
                ls = base.GetListBy(u => u.ConID == u.ConID);
                if (ls != null & ls.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.total = ls.Count;
                    result.rows = ls;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                #endregion
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 查询订单状态控制表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }


        /// <summary>
        /// 根据条件查询状态表
        /// </summary>
        /// <param name="FormID">订单ID</param>
        /// <param name="Componen">模块ID</param>
        /// <param name="tag">0--指定模块ID的所有状态,这时的订单ID入参可为空    1--查询指定模块下这个订单的下一个状态     2--指定模块ID的所有状态,以及这些状态的上一个状态</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string GetFromConID(int FormID, string Componen, int tag, string YZM, int RETID)
        {
            try 
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion

                // 1.前端传入两个参数：订单ID,模块ID
                //2.到数据库找该订单的FormBy字段
                //3. 有模块ID  &&  大于等于当前订单状态的 状态ID

                List<Model.Order_FormControl_T> conList = new List<Model.Order_FormControl_T>();

                switch (tag)
                {
                    case 0:
                        if (string.IsNullOrEmpty(Componen))
                        {
                            result.BS = "-1";
                            result.Msg = "没有模块ID";
                            return os.StringOfObject(result, 1);
                        }
                        List<Model.Order_FormControl_T> frmCon = base.GetListBy("select * from  Order_FormControl_T where ComponenGroup like '%"+Componen+"%' and ConState=1");//u => u.ComponenGroup.Contains(Componen) & u.ConState == 1);
                        if (frmCon.Count > 0)
                        {
                            foreach (Model.Order_FormControl_T item in frmCon)
                            {
                                string[] comps = item.ComponenGroup.Split(',');
                                List<string> com = comps.Where(u => u == Componen).ToList();
                                if (com.Count > 0)
                                {
                                    conList.Add(item);
                                }
                            }
                        }
                        break;

                    case 1:
                        if (string.IsNullOrEmpty(Componen))
                        {
                            result.BS = "-1";
                            result.Msg = "没有模块ID";
                            return os.StringOfObject(result, 1);
                        }
                        List<Model.Order_Form_T> ofList = base.DBSession.IOrder_Form_TDAL.GetListBy(" select * from   Order_Form_T where FormID="+FormID);//u => u.FormID == FormID & u.FormState == 1);
                        if (ofList.Count <= 0)
                        {
                            result.BS = "-1";
                            result.Msg = "找不到这个订单";
                            return os.StringOfObject(result, 1);
                        }

                        Model.Order_Form_T of = ofList.First();
                        List<Model.Order_FormControl_T> fmcon = base.GetListBy("select * from  Order_FormControl_T where ConID='"+of.FormBY+"'  and  ConState = 1 ");//  u => u.ConID == of.FormBY & u.ConState == 1 );
                        string nextcon = fmcon.First().NextCon;
                        if (!string.IsNullOrWhiteSpace(fmcon.First().NextCon))
                        {
                            List<string> nextList = nextcon.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                            foreach (var nxt in nextList)
                            {
                                List<Model.Order_FormControl_T> fcList = base.GetListBy("select * from  Order_FormControl_T where ConID='"+nxt +"'  and ConState = 1 ");//u => u.ConID == nxt & u.ConState == 1).ToList();

                                foreach (var fc in fcList )
                                { 
                                    int com = conList.Where(u => u.ConID == fcList.First().ConID).ToList().Count;
                                    string[] fcom = fc.ComponenGroup.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    int cnt = fcom.Where(u => u == Componen).ToList().Count;
                                    if (com == 0 && cnt>0 )//&& fc.ComponenGroup.Contains(Componen))
                                    {
                                        conList.Add(fcList.First());
                                    }
                                }
                            }
                            

                        }
                        break;

                    case 2:
                        //if (string.IsNullOrEmpty(Componen))
                        //{
                        //    result.BS = "-1";
                        //    result.Msg = "没有模块ID";
                        //    return os.StringOfObject(result, 1);
                        //}

                        List<Model.Order_FormControl_T> nowList = new List<Model.Order_FormControl_T>();
                        List<Model.Order_FormControl_T> flist = base.GetListBy(u => u.ComponenGroup.Contains(Componen) & u.ConState == 1);
                        if (flist.Count > 0)
                        {
                            foreach (Model.Order_FormControl_T item in flist)
                            {
                                string[] comps = item.ComponenGroup.Split(',');
                                List<string> com = comps.Where(u => u == Componen).ToList();
                                if (com.Count > 0)
                                {
                                    nowList.Add(item);
                                }
                            }
                        }
                        if (nowList.Count > 0)//查询当前状态的上一个状态
                        {
                            conList.AddRange(nowList);
                            foreach (var item in nowList)
                            {
                                List<Model.Order_FormControl_T> lastList = base.GetListBy(u => u.NextCon.Contains(item.ConID));

                                foreach (var lat in lastList)
                                {
                                    List<string> latnext = lat.NextCon.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                                    int nxtcount = latnext.Where(u => u == item.ConID).ToList().Count;
                                    if (nxtcount > 0)
                                    {
                                        int cnt = conList.Where(u => u.ConID == lat.ConID).ToList().Count;
                                        if (cnt <= 0)
                                        {
                                            conList.Add(lat);
                                        }

                                    }
                                }

                                //if (!string.IsNullOrWhiteSpace(item.NextCon))
                                //{
                                //string[] nextCon = item.NextCon.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                //foreach (var nxt in nextCon)
                                //{
                                //    List<Model.Order_FormControl_T> lastList = base.GetListBy(u => u.ConID ==nxt);
                                //    if (lastList.Count > 0)
                                //    {
                                //        foreach (var lat in lastList)
                                //        {
                                //            int cnt = conList.Where(u => u.ConID == lat.ConID).ToList().Count;
                                //            if (cnt == 0)
                                //            {
                                //                conList.Add(lat);
                                //            }
                                //        }

                                //    }
                                //}



                                //}

                            }

                        }
                        break;
                }

                if (conList.Count == 0)
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                else
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.total = conList.Count;
                    result.rows = conList;
                }

            }
            catch (Exception e)
            {
                result.BS = "-99";
                result.Msg = "系统异常";
            }

            return os.StringOfObject(result, 1);
        }


        /// <summary>
        /// 得到当前订单所有的状态
        /// </summary>
        /// <param name="FormID">订单ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string GetOrder_FormLog(int FormID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion

                List<Model.Order_FormControl_T> oft = base.GetListBy(u => true);
                List<Model.Order_FormLog> flog = base.DBSession.IOrder_FormLogDAL.GetListBy(" select * from   Order_FormLog  where FormID=" + FormID + "  order by  OpearDateTime asc");

                List<Model.FormInfo> resultList = new List<Model.FormInfo>();

                Model.View_Order_Form oderf = base.DBSession.IView_Order_FormDAL.GetListBy(u => u.FormID == FormID).First();

                foreach (var item in flog)
                {
                    FormInfo fif = new FormInfo();
                    switch (item.OperaType)
                    {
                        case 0:
                            Model.View_User user = base.DBSession.IView_UserDAL.GetListBy(u => u.AccID == item.OperaId).First();
                            fif.Tip = "买家";
                            fif.Name = user.AccName;
                            fif.Call = user.AccCall;
                            break;
                        case 1:
                            Model.Order_Mer_T mer = base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerID == oderf.FormMerID).First();
                            fif.Tip = "商家";
                            fif.Name = oderf.MerName;
                            fif.Call = mer.MerCall;
                            fif.PreTime = oderf.SenTime;
                            break;
                        case 2:
                            Model.Order_Taker_T taker = base.DBSession.IOrder_Taker_TDAL.GetListBy(u => u.TakerID == item.OperaId).First();
                            fif.Name = taker.TakerName;
                            fif.Call = taker.TakerPhone;
                            fif.Tip = "送餐员";
                            fif.PreTime = oderf.SenTime;
                            break;
                    }
                    fif.LogTime = item.OpearDateTime;
                    fif.ConText = oft.Where(u => u.ConID == item.FormStat).First().ConText;

                    resultList.Add(fif);
                }
                if (resultList.Count <= 0)
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                else
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.total = resultList.Count;
                    result.rows = resultList;
                }
            }
            catch (Exception ex)
            {
                result.BS = "-99";
                result.Msg = "系统异常！";
                WriteLog.WriteW("GetBeforeCon     得到当前订单状态以前所有的状态失败   " + ex.Message);
            }
            return os.StringOfObject(result, 1);
        }



    }
}
